<?php


namespace Tools\Utils;


use think\cache\driver\Redis;

class RedisStore
{
    const COUNTER = 15; //计数器DB
    const UPLOAD_CONN = 14; //图片上传连接池
    const RUNTIME_REPORT = 13; //记录运行报告
    const DATA_CACHE = 12; //数据缓存
    const REDIS_QUEUE = 11; //数据处理队列
    const REDIS_LOCK = 10; //Redis锁
    const PERSISTENT_DATA = 9; //数据持久化

    private static $map = [
        'default' => 0,
        'session' => 3,     //session存入db3
        'persister_data' => 9,
        'redis_lock' => 10,
        'queue' => 11,
        'data_cache' => 12,
        'runtime_report' => 13,
        'upload_conn' => 14,
        'counter' => 15,
    ];

    private static $default;
    private static $session;
    private static $persister_data;
    private static $redis_lock;
    private static $queue;
    private static $data_cache;
    private static $runtime_report;
    private static $upload_conn;
    private static $counter;

    private static $image_example;

    /**
     * use redis db 0
     * @return Redis
     * @throws \Exception
     */
    public static function instanceDefault()
    {
        return self::instance('default');
    }

    /**
     * use redis db 3
     * @@description  存储redis 3 号库的数据
     * @return RedisStore
     */
    public static function instanceSession()
    {
        return self::instance('session');
    }

    public static function instancePersisterData()
    {
        return self::instance('persister_data');
    }

    public static function instanceRedisLock()
    {
        return self::instance('redis_lock');
    }

    public static function instanceQueue()
    {
        return self::instance('queue');
    }

    public static function instanceDataCache()
    {
        return self::instance('data_cache');
    }

    public static function instanceRuntimeReport()
    {
        return self::instance('runtime_report');
    }

    public static function instanceUploadConn()
    {
        return self::instance('upload_conn');
    }

    public static function instancecounter()
    {
        return self::instance('counter');
    }

    private static function instance($key, $option = [])
    {
        if (isset(self::$map[$key])) {
            if (self::${$key} instanceof RedisStore) {
                return self::${$key};
            } else {
                $options = array_merge([
                    'select' => self::$map[$key],
                    'host' => config('cache.stores.redis.host'),
                    'port' => config('cache.stores.redis.port'),
                    'password' => config('cache.stores.redis.password'),
                    'prefix' => config('cache.stores.redis.prefix'),
                ], $option);
                return self::${$key} = new Redis($options);
            }
        } else {
            throw new \Exception($key . ' is not in cunzan\util\ReidsStore $map or class attributions');
        }
    }

    public static function instanceImages()
    {
        if (self::$image_example instanceof RedisStore) {
            return self::$image_example;
        } else {
            $option = [
                'host' => config('cunzan.image.redis.host'),
                'port' => config('cunzan.image.redis.port'),
                'password' => config('cunzan.image.redis.password'),
                'select' => 12,
                'prefix' => 'image'
            ];
            return self::$image_example = new Redis($option);
        }
    }
}
